package com.zhp.ggweather.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.zhp.ggweather.app.App;

public class NetRequestUtil {

	public static String requestDataForResult(String url) {
		if (NetUtil.getNetworkState(App.getInstance()) == NetUtil.NETWORK_NONE)
			return null;
		String result="";
		InputStream is = null;
		ByteArrayOutputStream bos = null;
		try {
			URL mUrl = new URL(url);
			HttpURLConnection connect = (HttpURLConnection) mUrl
					.openConnection();
			connect.setConnectTimeout(15 * 1000);
			connect.setReadTimeout(15 * 1000);
			connect.setRequestMethod("POST");
			is = connect.getInputStream();
			if (connect.getResponseCode() == HttpURLConnection.HTTP_OK) {
				byte[] buf = new byte[1024];
				int len = 0;
				bos = new ByteArrayOutputStream();
				while ((len = is.read(buf)) != -1) {
					bos.write(buf, 0, len);
				}
				result=bos.toString();
				System.out.println("查到的结果--->"+result);
				return result;
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (bos != null) {
					bos.close();
				}
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
}
